package com.bw.kf.all2211a.fg

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bw.kf.all2211a.R
import com.bw.kf.all2211a.adapter.MenuAdapter
import com.bw.kf.all2211a.adapter.MoneyAdapter
import com.bw.kf.all2211a.adapter.ProductAdapter
import com.bw.kf.all2211a.databinding.FragmentBlank1Binding
import com.bw.kf.all2211a.seatch.SearchActivity
import com.bw.kf.mvvm.base.BaseFragment
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader

class BlankFragment1 : BaseFragment<FragmentBlank1Binding,FgViewMode1>() {
   val imgs=mutableListOf<String>()
    lateinit var menuAdapter: MenuAdapter
    lateinit var moneyAdapter: MoneyAdapter
    lateinit var productAdapter: ProductAdapter
    var type=1
    var title=1
    override fun iniView() {
        viewModel.index()
        viewModel.product(1,5,type)
        productAdapter= ProductAdapter()
        menuAdapter= MenuAdapter()
        moneyAdapter= MoneyAdapter()
        binding.rvMoney.adapter=moneyAdapter
        binding.rvMoney.layoutManager=GridLayoutManager(context,4)
        binding.rvMenu.adapter=menuAdapter
        binding.rvMenu.layoutManager=GridLayoutManager(context,5)
        binding.rvGoods.adapter=productAdapter
        binding.rvGoods.layoutManager=GridLayoutManager(context,2)
         binding.sm.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
             override fun onRefresh(refreshLayout: RefreshLayout) {
                  type=1
                 viewModel.product(1,5,type)
                 binding.sm.finishRefresh()
             }

             override fun onLoadMore(refreshLayout: RefreshLayout) {
                  type+=1
                 viewModel.product(1,5,type)
                 binding.sm.finishLoadMore()
             }

         })
        moneyAdapter.setOnItemClickListener { adapter, view, position ->
            when(position){
                0->{
                    title=1
                    viewModel.product(title,20,1)
                    productAdapter.notifyDataSetChanged()

                }
                1->{
                    title=2
                    viewModel.product(title,20,1)
                    productAdapter.notifyDataSetChanged()

                }
                2->{
                    title=3
                    viewModel.product(title,20,1)
                    productAdapter.notifyDataSetChanged()

                }
                3->{
                    title=4
                    viewModel.product(title,20,1)
                    productAdapter.notifyDataSetChanged()

                }
            }
        }
     binding.souths.setOnClickListener {
         startActivity(Intent(context,SearchActivity::class.java))
     }
    }
    override fun iniData() {
        viewModel.success.observe(this){
            if(it.code==200){
          it.data.banner.forEach{
              imgs.add(it.pic)
          }
                binding.banner.setImages(imgs)
                    .setImageLoader(object : ImageLoader(){
                        override fun displayImage(
                            context: Context?,
                            path: Any?,
                            imageView: ImageView?
                        ) {
                            if(context!=null&&imageView!=null){
                                Glide.with(context).load(path).into(imageView)
                            }
                        }

                    })
                    .setBannerStyle(BannerConfig.NUM_INDICATOR)
                    .start()
                //菜单
                menuAdapter.data.clear()
                menuAdapter.data.addAll(it.data.menus)
                menuAdapter.notifyDataSetChanged()
                //分类
                 moneyAdapter.data.clear()
                moneyAdapter.data.addAll(it.data.explosiveMoney)
                moneyAdapter.notifyDataSetChanged()
            }
            ToastUtils.showLong(it.message)
        }
        viewModel.successP.observe(this){
            if(it.code==200){
                if(type==1){
                    productAdapter.data.clear()
                }
                productAdapter.data.addAll(it.data.list)
                productAdapter.notifyDataSetChanged()
                if(it.data.list.size==0){
                    ToastUtils.showLong("没有了")
                }
            }
            ToastUtils.showLong(it.message)
        }
    }
    override fun getViewModel(): Class<FgViewMode1> {
   return FgViewMode1::class.java
    }
    override fun Layoutid():Int=R.layout.fragment_blank1

}